package com.ideaaedi.zoo.example.springboot.mq.rocketmq;

import com.ideaaedi.zoo.diy.feature.msg.api.mq.MqFaceUtil;
import com.ideaaedi.zoo.diy.feature.msg.api.mq.entity.MqSendResult;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.messaging.support.MessageBuilder;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 消费者示例
 */
@Configuration
public class DemoProducer implements ApplicationRunner {
    
    /**
     * 生产者示例
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        MqSendResult result = MqFaceUtil.send("producerA-out-0",
                MessageBuilder.withPayload("1~")
                        .setHeader("traceId", UUID.randomUUID().toString())
                        .build());
        System.err.println("往producerA-out-0发送是否成功：" + result.isSuccess());
        
        Map<String, Object> header = new HashMap<>(8);
        header.put(MessageConst.PROPERTY_TAGS, "test-tag");
        Message<String> message = new GenericMessage<>("2~", header);
        result = MqFaceUtil.send("producerB-out-0", message);
        System.err.println("往producerB-out-0发送是否成功：" + result.isSuccess());
        
        
        // 测试标签
        header = new HashMap<>(8);
        header.put(MessageConst.PROPERTY_TAGS, "test-tag");
        message = new GenericMessage<>("3~", header);
        result = MqFaceUtil.send("producerC-out-0", message);
        System.err.println("往producerC-out-0发送是否成功：" + result.isSuccess());
        
        header.put(MessageConst.PROPERTY_TAGS, "abc-tag");
        message = new GenericMessage<>("4~", header);
        result = MqFaceUtil.send("producerC-out-0", message);
        System.err.println("往producerC-out-0发送是否成功：" + result.isSuccess());
    }
}